﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace miniProjSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamWriter writer = new StreamWriter(Console.OpenStandardOutput()); // Initialize writer to print to the console
            writer.AutoFlush = true; // Ensure that buffer is flushed
            writer.WriteLine("{0,10:X}", 42); // Prints '        A2'
            Console.WriteLine("{0,10:X}", 42); // Prints '        A2'
            writer.WriteLine(Convert.ToString(42, 2).PadLeft(10, '0')); // Prints '0000101010'
            writer.WriteLine(42, 16, 8); // Use extension method

            MyStream writer2 = new MyStream(Console.OpenStandardOutput());
            writer2.AutoFlush = true;
            writer2.WriteLine(42, 8, 8); // Use overloaded method in derived class
            //writer2.WriteLine(42, 20, 8); // <- Will compile but throws an invalid base exception

            Console.ReadKey();
        }
    }

    public class MyStream : StreamWriter
    {
        public MyStream(Stream inner)
            : base(inner)
        { 
        }

        public void WriteLine(int i, int printBase, int width, char padder = '0')
        {
            WriteLine(Convert.ToString(i, printBase).PadLeft(width, padder));
        }
    }

    public static class StreamExtender
    {
        public static void WriteLine(this StreamWriter stream, int i, int printBase, int width, char padder = '0')
        {
            stream.WriteLine(Convert.ToString(i, printBase).PadLeft(width, padder));
        }
    }
}
